package com.misboot.document.aspect.log;

import cn.ewsd.common.service.IMybatisBaseService;
import cn.hutool.core.convert.Convert;
import cn.hutool.extra.spring.SpringUtil;
import org.aspectj.lang.JoinPoint;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;


/**
 * 基础解析类
 * 单表编辑时可以直接使用id来查询
 * 如果为多表复杂逻辑，请自行编写具体实现类
 *
 * @Author 湖南佐佑时代科技有限公司（https://www.zuoyo.com）
 * @Version V5.4.21
 * @Email service@zuoyo.com
 * @Date 2023/03/28
 */
@Component
public class DefaultContentParse implements ContentParser {

    @Override
    public Object getOldResult(JoinPoint joinPoint, OperateLog enableModifyLog) {
        Object info = joinPoint.getArgs()[0];
        Object id = ReflectionUtils.getFieldValue(info, "uuid");
        Assert.notNull(id, "未解析到uuid值，请检查前台传递参数是否正确");
        Class idType = enableModifyLog.idType();
        if (idType.isInstance(id)) {
            Class cls = enableModifyLog.serviceClass();
            IMybatisBaseService service = (IMybatisBaseService) SpringUtil.getBean(cls);
            Object result = service.selectByPrimaryKey(Convert.toStr(idType.cast(id)));
            return result;
        } else {
            throw new RuntimeException("请核实uuid type");
        }
    }

    @Override
    public Object getNewResult(JoinPoint joinPoint, OperateLog enableModifyLog) {
        return getOldResult(joinPoint, enableModifyLog);
    }


}
